﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class FinanceCenter_EarningList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            lbUserName.Text = _Customer.CustomerName;

            tbStartTime.Attributes.Add("onclick", "WdatePicker()");
            tbEndTime.Attributes.Add("onclick", "WdatePicker()");

            DateTime dtime = DateTime.Now;
            tbStartTime.Value = DateTime.Parse(dtime.Year.ToString() + "-" + dtime.Month + "-01 00:00:00").ToString("yyyy-MM-dd");
            tbEndTime.Value = dtime.ToString("yyyy-MM-dd");

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        isAtFramePageLogin = true;                              //是框架页
        RequestLoginPage = this.Request.Url.AbsoluteUri;
        RequestCompetences = Competences.BuildCompetencesList(Competences.Finance);

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DateTime StartTime = Shove._Convert.StrToDateTime(tbStartTime.Value.ToString(), DateTime.Now.ToShortDateString());
        DateTime EndTime = Shove._Convert.StrToDateTime(tbEndTime.Value.ToString(), DateTime.Now.ToShortDateString());
        
        string ReturnDescription = "";

        DataSet ds = null;

        DAL.Procedures.P_InComingOrOutComing(ref ds, StartTime, EndTime, 1, ref ReturnDescription);  //1 收入 2 支出 3 充值

        if (ds == null)
        {
            return;
        }

        g.DataSource = ds.Tables[0].DefaultView;
        g.DataBind();

        gPager.Visible = (ds.Tables[0].Rows.Count > 0);

        if (ds.Tables[1].Rows.Count > 0)
        {
            string strMoney = ds.Tables[1].Rows[0]["SumMoney"].ToString();

            lbUserCount.Text = ds.Tables[1].Rows[0]["SumID"].ToString();
            lbBuyCount.Text = (Shove._Convert.StrToDouble(strMoney, 0) == 0) ? "0.00" : Shove._Convert.StrToDouble(strMoney, 0).ToString("N");            
        }     
    }

    protected void ibSelect_Click(object sender, ImageClickEventArgs e)
    {
        gPager.PageIndex = 0;

        BindData();
    }

    protected void ibPrint_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Write("<script language='javascript'>window.open('EarningListForPrint.aspx?StartTime=" + tbStartTime.Value.ToString() + "&EndTime=" + tbEndTime.Value.ToString() + "');</script>");
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            double money = Shove._Convert.StrToDouble(e.Item.Cells[3].Text, 0);
            e.Item.Cells[3].Text = (money == 0) ? "0" : money.ToString("N");

            string sequenceNumber = (e.Item.ItemIndex + 1).ToString();
            if (Shove._Convert.StrToInt(sequenceNumber, 0) >= 10)
            {
                e.Item.Cells[0].Text = "0" + sequenceNumber;
            }
            else
            {
                e.Item.Cells[0].Text = "00" + sequenceNumber;
            }
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }
}

